<?php
/**
 * 《物流公司》实现类
 *
 * @author 孙晓晔
 * @version $Id: Express.class.php 1059 2010-04-08 07:55:21Z sunxy $
 */
require_once(dirname(__FILE__) . '/Supplier.class.php');

class Express extends Supplier {
	var $aa = 3;
	var $prefix = "express.";
	
	public $orderby = array(
		'-' => '请选择',
		'code' => '编号',
		'name' => '名称',
		'star' => '等级',
		
		'category' => '分类',
	
		'1' => '创建时间',
		'2' => '更新时间',
	);
	
	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-7 month'));

		$controller->display($request, 'contact/express/find');
		return true;
	}

	function _where($request) {
		$code_s = $request['code_s'];
		$name_s = $request['name_s'];

		$star_s = $request['star_s'];

		$category_s = $request['category_s'];
		$type_s = $request['type_s'];

		$province_s = $request['province_s'];
		$city_s = $request['city_s'];
		$district_s = $request['district_s'];
		
		$where = " WHERE company = 3";

		if (!empty($code_s)) {
			$where .= " AND code = '" . $code_s . "'";
		}

		if (!empty($name_s)) {
			$where .= " AND name like '%" . $name_s . "%'";
		}

		if (!empty($star_s) && $star_s <> '-') {
			$where .= " AND star = '" . $star_s . "'";
		}

		if (!empty($category_s) && $category_s <> '-') {
			$where .= " AND category = '" . $category_s . "'";
		}

		if (!empty($type_s) && $type_s <> '-') {
			$where .= " AND type = '" . $type_s . "'";
		}

		if (!empty($province_s)) {
			$where .= " AND province like '%" . $province_s . "%'";
		}
		if (!empty($city_s)) {
			$where .= " AND city like '%" . $city_s . "%'";
		}
		if (!empty($district_s)) {
			$where .= " AND district like '%" . $district_s . "%'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'contact/express/list');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'contact/express/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$code = $request['code'];
		$name = addslashes($request['name']);

		$zjm = getPinyin($name);
		$zjm = strtoupper($zjm);
		$this->_zjm_clean($zjm);

		$fullname = addslashes($request['fullname']);
		$category = addslashes($request['category']);

		$address = addslashes($request['address']);

		$url = strtolower($request['url']);
		$jj = addslashes($request['jj']);

		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(company, name, zjm, fullname, category, address, url, jj, content, emp";
		$fmt .= ", createtime, modifytime) VALUES('3', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $name, $zjm, $fullname, $category, $address, $url, $jj, $content, $_SESSION['id']);
		debug($sql);
		$db->query($sql);

		$id = $db->insert_id();
		if (empty($code)) {
			$code = $this->_code($category, $id);
		}

		$contact = stripslashes($request['contact']);
		$this->_contact($id, $contact);

		$fmt = "UPDATE %s SET code = '%s' WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $code, $id);
		$db->query($sql);
		
		$request['id_s'] = $id;

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->assign_by_ref('contact', $this->_linkman($id));

		$controller->display($request, 'contact/express/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);

		$code = $request['code'];
		$name = addslashes($request['name']);

		$zjm = getPinyin($name);
		$zjm = strtoupper($zjm);
		$this->_zjm_clean($zjm);

		$fullname = addslashes($request['fullname']);
		$star = $request['star'];
		$category = addslashes($request['category']);

		$address = addslashes($request['address']);

		$url = strtolower($request['url']);
		$jj = addslashes($request['jj']);

		$content = addslashes($request['content']);

		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET name = '%s', zjm = '%s', fullname = '%s', star = '%s', category = '%s'";
		$fmt .= ", address = '%s', url = '%s', jj = '%s', content = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $name, $zjm, $fullname, $star, $category, $address, $url, $jj, $content, $status, $id);
		debug($sql);
		$db->query($sql);
		if (!$db->error()) {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));
		} else {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		if (empty($code)) {
			$code = $this->_code($category, $id);
		}

		$fmt = "UPDATE %s SET code = '%s' WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $code, $id);
		// debug($sql);
		$db->query($sql);

		$contact = stripslashes($request['contact']);
		$this->_contact_id($id, $contact);

		$z = stripslashes($request['z']);
		$this->_delete($z);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->assign_by_ref('contact', $this->_linkman($id));

		$controller->assign_by_ref('bank', $this->_bank($id));
		
		if (!empty($request['small'])) {
			$controller->display($request, 'contact/supplier/view_small');
			return true;
		}
		
		$controller->display($request, 'contact/express/view');
		return true;
	}

	function t($controller, $request) {

		logdate($controller, $request);
		return true;
	}
}
?>